package com.dashu.base.utils.encryption;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.io.IOException;
import java.nio.charset.Charset;

public class Base64 {

    /**
     * 编码
     * @param msg
     * @return
     */
    public static String Encoder(String msg){
        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(msg.getBytes(Charset.forName("utf-8")));
    }


    /**
     * 编码
     * @param msg
     * @return
     */
    public static String Encoder(byte[] msg){
        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(msg);
    }

    /**
     * 解码
     * @param msg
     * @return
     */
    public static byte[] Decoder(byte[] msg){
        BASE64Decoder decoder = new BASE64Decoder();
        try {
            return decoder.decodeBuffer(new String(msg , Charset.forName("utf-8")));
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }


    /**
     * 解码
     * @param msg
     * @return
     */
    public static String Decoder(String msg){
        BASE64Decoder decoder = new BASE64Decoder();
        try {
            return new String(decoder.decodeBuffer(msg) , Charset.forName("utf-8"));
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }


}
